<template>
    <div class="box">
        <van-nav-bar title="客户信息" class="navBar" left-arrow @click-left="back()" right-text="帮助"></van-nav-bar>
        <van-search
            v-model="searchValue"
            show-action
            action-text="搜索"
            placeholder="用户手机号/会员卡号/姓名"
            @search="onSearch"
        >
            <template #action>
                <div style="width: 1.87rem;">
                    <van-uploader
                        capture="camera"
                        :after-read="afterRead"
                        :show-upload="true"
                        style="position: absolute; right: 60px; z-index: 999;margin-top:.1875rem"
                    >
                        <van-icon name="scan" />
                    </van-uploader>
                    <div @click="onSearch" style="float: right">搜索</div>
                </div>
            </template>
        </van-search>
        <van-tabs v-model:active="active" sticky @click-tab="onClickTab" class="tabBox">
            <van-tab title="客户信息"><User :userInfo="param.userInfo"/></van-tab>
            <van-tab title="车辆信息"><Car :carInfo="param.carInfo"/></van-tab>
            <van-tab title="套餐剩余"><Combo :comboInfo="param.comboInfo"/></van-tab>
            <van-tab title="消费记录"><Record :recordInfo="param.recordInfo"/></van-tab>
            <van-tab title="维修单"><Fixed :fixedInfo="param.fixedInfo"/></van-tab>
        </van-tabs>
    </div>
</template>
<script setup>
    import User from './user.vue'
    import Car from './car.vue'
    import Combo from './combo.vue'
    import Fixed from './fixed.vue'
    import Record from './record.vue'
    import {ref,reactive} from 'vue';
    import { useRouter } from 'vue-router'
    import {queryUserInfo,queryFixedLix,queryCarInfo} from 'api/customer.js'
    import {orderList,piabjkProjectList} from 'api/sales.js'

    const searchValue= ref();
    const router = useRouter();
    const param = reactive({
        userInfo:{},carInfo:[],comboInfo:[],recordInfo:[],fixedInfo:[]
    })
    let globalTitle=ref("客户信息");
    const active = ref(0)

    const onSearch = () =>{
        console.log('搜索')
        search(globalTitle);
    }
    const onClickTab = ({name,title,event,disabled}) =>{
        console.log(name,title,event)
        globalTitle=title;
        search(globalTitle)
    }

    const search = (title) => {
        if(searchValue.value?.length!=11){
            return;
        }
        console.log(title)
        switch(title){
            case '客户信息':
                queryUser(searchValue.value)
                break;
            case '车辆信息':
                queryCar(searchValue.value)
                break;
            case '套餐剩余':
                queryCombo(searchValue.value)
                break;
            case '消费记录':
                queryRecord(searchValue.value)
                break;
            case '维修单':
                queryFixed(searchValue.value)
                break;
            default:
                queryUser(searchValue.value)
                break;
        }
    }

    const queryUser = async(phone)=>{
            let res = await queryUserInfo({phone})
            let data = res.data[0];
            param.userInfo=data;
            console.log(data);
    }
    const queryCar = async(phone)=>{
        let res = await queryCarInfo({phone})
        let data = res.data;
        param.carInfo=data;
        console.log(data);
    }
    const queryCombo = async(phone)=>{
        let res = await orderList({tel:phone,page:1,limit:200,piabjkStatus:1})
        let values = res.data;
        console.log(values)
        for(let i =0;i<values.length;i++){
            let v= values[i];
            v.products = JSON.parse(v.products)
            let cancelRes = await piabjkProjectList({orderId:v.orderId,page:1,limit:200})
            console.log(cancelRes);
            v['cancelList'] = cancelRes.data;
        }
        param.comboInfo=values;
        console.log(values)
    }
    const queryRecord =async(phone)=>{
        let res = await orderList({tel:phone,page:1,limit:200})
        let data = res.data;
        param.recordInfo=data;
        console.log(data);
    }
    const queryFixed = async(phone)=>{
        let res = await queryFixedLix({phone,pageSize:200,pageNum:1})
        let data = res.data;
        param.fixedInfo=data.result.list;
        console.log(data);
    }


    
    const back = () => {
        router.push('/home')
    }
    const afterRead = (file) => {
        console.log("扫描结果准备处理");
        getUrl(file.file);
    };

    function getUrl(file) {
        let url = getObjectURL(file);
        qrcode.decode(url);
        qrcode.callback = function (imgMsg) {
            console.log("reqrcode result:", imgMsg);
            console.log("订单号：", imgMsg);
            if (!imgMsg) {
                Toast.fail("识别失败");
                return;
            }
            searchValue.value = imgMsg;
            onSearch();
        };
    }
    function getObjectURL(file) {
        let url = null;
        if (window.createObjectURL !== undefined) {
            // basic
            url = window.createObjectURL(file);
        } else if (window.URL !== undefined) {
            // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL !== undefined) {
            // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
</script>
<style scoped  lang="scss">
    .box{
        background: #F1F5F8;
        height: 100vh;
        .navBar{
            background: #fff;
        }
    }
</style>
<style >
.customerSearch  .van-search__content{
    border-radius: 1rem /* 16/16 */ !important;
}
.tabBox .van-tab--active{
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #000000;
}
.tabBox .van-tab--active{
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: bold!important;
    color: #000000;
}
.tabBox .van-tabs__line{
    width: 19px;
    height: 4px;
    background: #5E2F88;
    border-radius: 4px;

}
</style>